const access_token = '_course-access-token';
const refresh_token = '_course-refresh-token';

export default function stoken(ctx, mode) {
    Object.defineProperty(ctx, 'access_token', {
        get: () => {
            return ctx.cookies.get(access_token, {
                signed: true,
                httpOnly: true
            });
        },
        set: (value) => {
            if(!value){
                ctx.cookies.set(access_token, value, {
                    signed: false,
                    maxAge: 0,
                });
            }else{
                ctx.cookies.set(access_token, value, {
                    signed: true,
                    maxAge: 5 * 60 * 60 * 1000,
                    httpOnly: true,
                    overwrite:true
                });
            }
            
        }
    });
     Object.defineProperty(ctx, 'refresh_token', {
        get: () => {
            return ctx.cookies.get(refresh_token, {
                signed: true,
                httpOnly: true
            });
        },
        set: (value) => {
            if(!value){
                ctx.cookies.set(refresh_token, value, {
                    signed: false,
                    maxAge: 0,
                });
            }else{
                ctx.cookies.set(refresh_token, value, {
                    signed: true,
                    maxAge: 5 * 60 * 60 * 1000,
                    httpOnly: true,
                    overwrite:true
                });
            }
            
        }
    });


    ctx.sess = async () => {
        // let result = await ctx.get("/users/userInfo");
        // if(result.errorCode != 0){
        //     return 
        // }
        // return result.data||{};
        return {};
    };
    ctx.clearSess = () =>{
        ctx.access_token = null;
        ctx.refresh_token = null;
    }
}
